package com.feige.controller;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.feige.pojo.NotifyMsgUser;
import com.feige.pojo.vo.NotifyMsgVo;
import com.feige.result.AjaxResult;
import com.feige.service.NotifyMsgService;
import com.feige.service.NotifyMsgUserService;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * <p>
 * 通知消息表 前端控制器
 * </p>
 *
 * @author feige
 * @since 2020-12-26
 */
@Api(tags = "通知消息表 前端控制器")
@RestController
@RequestMapping("/course/notify-msg")
public class NotifyMsgController {
    @Resource
    private NotifyMsgService notifyMsgService;

    @Resource
    private NotifyMsgUserService notifyMsgUserService;

    @GetMapping("/get/{pageNum}/{userId}")
    public AjaxResult getNotifyByUserId(@PathVariable String userId, @PathVariable Integer pageNum){
        List<NotifyMsgVo> notifyByUserId = notifyMsgService.getNotifyByUserId(pageNum, userId);
        int total = notifyMsgService.getNotifyCountByUserId(userId);
        AjaxResult success = AjaxResult.success(notifyByUserId);
        success.put("total",total);
        return success;
    }

    @PostMapping("/update")
    public AjaxResult updateNotifyMsgUser(@RequestBody NotifyMsgUser notifyMsgUser){
        boolean b;
        if (ObjectUtil.isNull(notifyMsgUser.getSignFlag())){
            UpdateWrapper<NotifyMsgUser> wrapper = new UpdateWrapper<>();
            wrapper
                    .eq("notify_msg_id",notifyMsgUser.getNotifyMsgId())
                    .eq("user_id",notifyMsgUser.getUserId());
            notifyMsgUser.setUserId(null);
            notifyMsgUser.setNotifyMsgId(null);
            b = notifyMsgUserService.update(notifyMsgUser, wrapper);
        }else {
            b = notifyMsgUserService.save(notifyMsgUser);
        }
        if (b){
            return AjaxResult.success();
        }else {
            return AjaxResult.error();
        }
    }
}

